#include "mainwindow.h"
#include <QApplication>
#include <QTextStream>
#include <QtCore/QCoreApplication>
#include <stdio.h>
#include "Queue.h"
#include "seat.h"
#include "ChessNode.h"
using namespace std;


//注：本程序只实现了核心算法和跳马路径的直观展示，交互功能未能成功实现，需要在程序中手动更改起始坐标位置


QTextStream cin(stdin,  QIODevice::ReadOnly);
//QTextStream cout(stdout,  QIODevice::WriteOnly);

int level[8]= {-2,-2,-1,-1,1,1,2,2};    //************************水平方向马走一步的增量
int high[8]= {-1,1,-2,2,-2,2,-1,1};    //*************************竖直方向马走一步的增量
seat start,last;    //********************************************始末位置
int board[51][51]= {0};    //*************************************棋盘
Queue que;   //***************************************************遍历过程存储结点的队列


bool operator==(const seat& left,const seat& right);

ChessNode* search_(ChessNode* root);    //************************寻找函数，返回seat为末位置的节点

void clear_();    //**********************************************清空棋盘，为下一轮做准备

void Display(ChessNode* root);    //******************************输出所需步数以及路径


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.resize(1400,1000);

    ChessNode* result;

    int x1=12,y1=48,x2=23,y2=5;      //****************************只能手动输入点，还未实现交互

    start = seat(x1,y1);
    last = seat(x2,y2);
    board[start.x][start.y]=1;

    ChessNode* r= new ChessNode(start.x,start.y,0);
    result=search_(r);
    clear_();


    if(result != NULL)
    {
        int time = result->floor+1;

        ChessNode* temp = result;
        for(int i=0; i<time; i++)
        {

            w.matrix[temp->chess.x][temp->chess.y]->type = 0;
            temp=temp->father;
        }
    }

    w.matrix[start.x][start.y]->type = 2;
    w.matrix[last.x][last.y]->type = 3;
    w.display();
    w.show();

    return a.exec();
}


void ChessNode::setchild()
{
    for(int i=0; i<8; i++)
    {
        if(chess.x+level[i]<50 && chess.y+high[i]<50 && chess.x+level[i]>0 && chess.y+high[i]>0)
            child[i]=new ChessNode(chess.x+level[i],chess.y+high[i],this->floor+1,this);
        else
            child[i]=NULL;
    }
}



bool operator==(const seat& left,const seat& right)
{
    if(left.x == right.x && left.y == right.y)
        return true;
    else
        return false;
}

ChessNode* search_(ChessNode* root)
{
    root->setchild();
    for(int i=0; i<8; i++)
    {
        if(root->child[i]!=NULL)
            if(board[root->child[i]->chess.x][root->child[i]->chess.y]==0)
            {
                board[root->child[i]->chess.x][root->child[i]->chess.y]=1;
                que.push(root->child[i]);
            }
    }
    if(root->chess == last)
        return root;
    else if(que.empty_())
    {

        return NULL;
    }
    else
    {
        ChessNode* temp = que.front_();
        que.pop();
        return search_(temp);

    }
}

void clear_()    //清空棋盘
{
    for(int i=0; i<50; i++)
    {
        for(int j=0; j<50; j++)
            board[i][j]=0;
    }
    while(!que.empty_())
    {
        que.pop();
    }
}


